﻿using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;


namespace GUI
{
    public partial class FormThemXoaSuaLichDi : Form
    {
        public FormThemXoaSuaLichDi()
        {
            InitializeComponent();
        }

        private void dtpNgay_ValueChanged(object sender, EventArgs e)
        {

        }

        private void FormThemXoaSuaLichDi_Load(object sender, EventArgs e)
        {
            List<LoaiXeDTO> mc = LoaiXeBUS.SelectALLLoaiXe();
            foreach (var item in mc)
            {
                this.cbbMaLoai.Items.Add(item.MALOAI);
            }
            List<ChuyenXeDTO> cx = ChuyenXeBUS.SelectALL();
            foreach (var item in cx)
            {
                this.cbbmachuyen.Items.Add(item.MACHUYEN);
            }
            this.dataGridView1.DataSource = LichDiBUS.SelectALLLichDi();
            List<String> tinhtrang= new List<string>{"YES","NO"};
            this.cbbTinhTrang.DataSource = tinhtrang;
        }

        private void btnthem_Click(object sender, System.EventArgs e)
        {
            String ngaydi = dtpNgay.Value.Year.ToString() + "/" + dtpNgay.Value.Month.ToString() + "/" + dtpNgay.Value.Day.ToString();
            String machuyen = cbbmachuyen.Text;
            String sohieu = cbbSoHieu.Text;
            String maloai = cbbMaLoai.Text;
            int sotai = Convert.ToInt32(txtSoTai.Text);
            int solo = Convert.ToInt32(txtSoLo.Text);
            int tthientai= Convert.ToInt32(txtTrongTai.Text);
            String tinhtrang = cbbTinhTrang.Text;
            try
            {
                BUS.LichDiBUS.ThemLichDi(ngaydi,machuyen,sohieu,maloai,sotai,solo,tthientai,tinhtrang);
                MessageBox.Show("Insert Successfull!!");
                FormThemXoaSuaLichDi_Load(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            dtpNgay.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
            cbbmachuyen.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            cbbSoHieu.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            cbbMaLoai.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
            txtSoTai.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            txtSoLo.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            cbbTinhTrang.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            txtTrongTai.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
        }

       private void btnsua_Click(object sender, System.EventArgs e)
       {
           String ngaydi = dtpNgay.Value.Year.ToString() + "/" + dtpNgay.Value.Month.ToString() + "/" + dtpNgay.Value.Day.ToString();
           String machuyen = cbbmachuyen.Text;
           String sohieu = cbbSoHieu.Text;
           String maloai = cbbMaLoai.Text;
           int sotai = Convert.ToInt32(txtSoTai.Text);
           int solo = Convert.ToInt32(txtSoLo.Text);
           int tthientai = Convert.ToInt32(txtTrongTai.Text);
           String tinhtrang = cbbTinhTrang.Text;
           try
           {
               BUS.LichDiBUS.SuaLichDi(ngaydi, machuyen, sohieu, maloai, sotai, solo, tthientai, tinhtrang);
               MessageBox.Show("Update Successfull!!");
               FormThemXoaSuaLichDi_Load(null, null);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void btnxoa_Click(object sender, System.EventArgs e)
       {
           String ngaydi = dtpNgay.Value.Year.ToString() + "/" + dtpNgay.Value.Month.ToString() + "/" + dtpNgay.Value.Day.ToString();
           String machuyen = cbbmachuyen.Text;
           String sohieu = cbbSoHieu.Text;
           try
           {
               BUS.LichDiBUS.XoaLichDi(ngaydi, machuyen, sohieu);
               MessageBox.Show("DELETE Successfull!!");
               FormThemXoaSuaLichDi_Load(null, null);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void cbbMaLoai_SelectedIndexChanged(object sender, System.EventArgs e)
       {
           String maloai = this.cbbMaLoai.Text;
           List<XeDTO> list = BUS.XeBUS.SelectSoHieuTheoMaLoai(maloai);
           this.cbbSoHieu.DataSource = list;
           cbbSoHieu.DisplayMember = "SOHIEU";
           cbbSoHieu.ValueMember = "SOHIEU";
       }
    }
}
